<?php

class process_payment extends a_base_controller_l_checkout {

    protected $pm;
    protected $drivers;

    public function __construct() {
        parent::__construct();
        $this->pm = new payment_manager();
        $this->drivers = $this->pm->load_enabled_drivers("1", "cad"); //@TO_DO currency/language come from a core function
        if (count($this->drivers) <= 0) {
            show_error("No payment method found.There must be atleast one active payment method.");
        }
        $this->assign("drivers", $this->drivers);
        $this->assign("payment_driver_selection", $this->input->post("driver") ? $this->input->post("driver") : "");
    }

    public function __handle_executed_before($is_post_request) {
        $this->view("process_payment");
    }

    public function __handle_executed_never($is_post_request) {
        if ($is_post_request) {
            $driver_id = $this->input->post("driver");
            $driver = @$this->drivers[$driver_id];
            if ($driver) {
                if ($driver->process($this->order_object)) {
//                    event_dispatcher::i()->fire(module_u_cart::ORDER_IS_PAID_E_CART);   //event added here
//
//                    $objMySession = get_instance()->session;                        // grabs the session object
//                    $objMySession->unset_userdata(session_cart_l_cart::i());        //unsets the session
//
//                    unset_userdata('');//remove cart session, ->destroy()
                    $this->stage_executed();
                    $this->next();
                } else {
                    $this->add_checkout_msg($driver->get_error_message());
                    $this->view("process_payment");
                }
            } else {
                $this->add_checkout_msg("Please select a payment option!");
                $this->view("process_payment");
            }
        } else {
            $this->view("process_payment");
        }
    }

    public function __handle_invalid_stage(stage_l_checkout $valid_stage) {
        redirect($valid_stage->get_action_url());
    }

    public function stage_id() {
        return "payment";
    }

}

?>